package api

import (
	"context"
	"fmt"
	"hxz_api/basic/globar"
	"hxz_api/handler/request"
	"hxz_rpc/kitex_gen/hxz_rpc/user"
	"io/ioutil"
	"net/http"
	"net/url"
	"time"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/dgrijalva/jwt-go"
	"github.com/skip2/go-qrcode"
	"github.com/zhangcook/jwt_go"
)

func Register(ctx context.Context, c *app.RequestContext) {
	var req request.UserReq
	if err := c.Bind(&req); err != nil {
		c.JSON(http.StatusInternalServerError, utils.H{
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	userRegister, err := globar.User.GetUserRegister(ctx, &user.GetUserRegisterReq{
		Mobile:   req.Mobile,
		Password: req.Password,
		Status:   req.Status,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, utils.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, utils.H{
		"code":    http.StatusOK,
		"message": "注册成功",
		"data":    userRegister,
	})
}

func Sgin(ctx context.Context, c *app.RequestContext) {
	appId := "wx736fff91235270fd"
	uri := "ffb86a241812e0bfc1837b403dae32bf"
	code := "111"
	codeUrl := fmt.Sprintf("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=%d&scope=SCOPE&state=STATE#wechat_redirect", appId, uri, code)
	fmt.Println(codeUrl)
	png, err := qrcode.Encode("https://example.org", qrcode.Medium, 256)
	if err != nil {
		return
	}
	c.Header("Content-Type", "image/png")
	c.Write(png)
}

func UserList(ctx context.Context, c *app.RequestContext) {
	var req request.UserReq
	if err := c.Bind(&req); err != nil {
		c.JSON(http.StatusInternalServerError, utils.H{
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	userRegister, err := globar.User.GetUserList(ctx, &user.GetUserListReq{
		Mobile: req.Mobile,
		Status: req.Status,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, utils.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, utils.H{
		"code":    http.StatusOK,
		"message": "查看成功",
		"data":    userRegister,
	})
}

func Position(ctx context.Context, c *app.RequestContext) {
	// 此处填写您在控制台-应用管理-创建应用后获取的AK
	ak := "bsSryvTCCuT2Xsetr5UIBMXzOxNL6Ih9"

	// 服务地址
	host := "https://api.map.baidu.com"

	// 接口地址
	uri := "/reverse_geocoding/v3/"

	// 设置请求参数
	params := url.Values{
		"ak":             []string{ak},
		"output":         []string{"json"},
		"coordtype":      []string{"bd09ll"},
		"extensions_poi": []string{"1"},
		"location":       []string{"39.951335108535,116.51484487905"},
	}

	// 发起请求
	request, err := url.Parse(host + uri + "?" + params.Encode())
	if nil != err {
		fmt.Printf("host error: %v", err)
		return
	}

	resp, err1 := http.Get(request.String())
	fmt.Printf("url: %s\n", request.String())
	defer resp.Body.Close()
	if err1 != nil {
		fmt.Printf("request error: %v", err1)
		return
	}
	body, err2 := ioutil.ReadAll(resp.Body)
	if err2 != nil {
		fmt.Printf("response error: %v", err2)
	}

	fmt.Println(string(body))

	return

}

func Login(ctx context.Context, c *app.RequestContext) {
	var req request.UserReq
	if err := c.Bind(&req); err != nil {
		c.JSON(http.StatusInternalServerError, utils.H{
			"code":    http.StatusInternalServerError,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	login, err := globar.User.GetUserLogin(ctx, &user.GetUserLoginReq{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, utils.H{
			"code":    http.StatusBadRequest,
			"message": err.Error(),
			"data":    false,
		})
		return
	}
	token, err := jwt_go.NewAuthenticationInformation("2302a").CreateToken(jwt_go.AuthenticationInformation{
		UserId:                uint(login.Id),
		PermissionInformation: nil,
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() * 3600 * 24,
		},
	})
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, utils.H{
		"code":    http.StatusOK,
		"message": "登录成功",
		"data":    token,
	})
}
